//
//  TeachSignerTwoCell.m
//  Coach_HaiGeEr
//
//  Created by liuchen on 2018/12/28.
//  Copyright © 2018年 海格尔. All rights reserved.
//

#import "TeachSignerTwoCell.h"
#import "UIImageView+WebCache.h"
@interface TeachSignerTwoCell()
@property (nonatomic,weak) UIImageView *headImgView;
@property (nonatomic,weak) UILabel *signTimeLabel;
@property (nonatomic,weak) UILabel *nameLabel;
@property (nonatomic,weak) UILabel *phoneLabel;
@property (nonatomic,weak) UIButton *phoneBtn;
@end

@implementation TeachSignerTwoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return self;
}

#pragma mark ==========创建视图==========
- (void)createSubView{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    UIImageView *headImgView = [[UIImageView alloc]init];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 110*SCALEUNIT, 110*SCALEUNIT) byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(55*SCALEUNIT,55*SCALEUNIT)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = CGRectMake(0, 0, 110*SCALEUNIT, 110*SCALEUNIT);
    maskLayer.path = maskPath.CGPath;
    headImgView.layer.mask = maskLayer;
    _headImgView = headImgView;
    [self addSubview:self.headImgView];
    
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.textAlignment = NSTextAlignmentLeft;
    nameLabel.font = [UIFont systemFontOfSize:LoginFont];
    nameLabel.textColor = [UIColor colorWithHexString:@"#282828"];
    _nameLabel = nameLabel;
    [self addSubview:self.nameLabel];
    
    UILabel *signTimeLabel = [[UILabel alloc] init];
    signTimeLabel.textAlignment = NSTextAlignmentRight;
    signTimeLabel.textColor = [UIColor colorWithHexString:@"#666666"];
    signTimeLabel.font = [UIFont systemFontOfSize:LoginFont-3];
    _signTimeLabel = signTimeLabel;
    [self addSubview:self.signTimeLabel];
    
    UILabel *phoneLabel = [[UILabel alloc] init];
    phoneLabel.textAlignment = NSTextAlignmentLeft;
    phoneLabel.font = [UIFont systemFontOfSize:LoginFont-3];
    phoneLabel.textColor = [UIColor colorWithHexString:@"#666666"];
    _phoneLabel = phoneLabel;
    [self addSubview:self.phoneLabel];
    
    UIButton *phoneBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [phoneBtn setBackgroundImage:[UIImage imageNamed:@"TeachManagementTelephoneImage"] forState:UIControlStateNormal];
    [phoneBtn addTarget:self action:@selector(phoneOpen:) forControlEvents:UIControlEventTouchUpInside];
    _phoneBtn = phoneBtn;
    [self addSubview:self.phoneBtn];
    
    //布局
    WS(ws);
    [self.headImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(ws).with.offset(30*SCALEUNIT);
        make.size.mas_equalTo(CGSizeMake(110*SCALEUNIT, 110*SCALEUNIT));
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.headImgView).with.offset(-5*SCALEUNIT);
        make.left.mas_equalTo(ws.headImgView.mas_right).with.offset(10*SCALEUNIT);
        make.size.mas_equalTo(CGSizeMake(325*SCALEUNIT, 60*SCALEUNIT));
    }];
    [self.signTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.nameLabel.mas_right);
        make.right.mas_equalTo(ws).with.offset(-55*SCALEUNIT);
        make.centerY.mas_equalTo(ws.nameLabel);
        make.height.mas_equalTo(55*SCALEUNIT);
    }];
    [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(ws.headImgView).with.offset(10*SCALEUNIT);
        make.left.mas_equalTo(ws.nameLabel);
        make.size.mas_equalTo(CGSizeMake(325*SCALEUNIT, 55*SCALEUNIT));
    }];
    [self.phoneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(ws.signTimeLabel);
        make.centerY.mas_equalTo(ws.phoneLabel);
        make.size.mas_equalTo(CGSizeMake(54*SCALEUNIT, 54*SCALEUNIT));
    }];
}

- (void)setModel:(TeachManagementModel *)model{
    _model = model;
    [self.headImgView sd_setImageWithURL:[NSURL URLWithString:_model.imgPath] placeholderImage:[UIImage imageNamed:@"MineBoyImage"]];
    self.nameLabel.text = _model.studentName;
    self.signTimeLabel.attributedText = [self returnAttributeWithTitle:[[_model.asignCoachDatetime componentsSeparatedByString:@" "] lastObject]];
    self.phoneLabel.text = [NSString stringWithFormat:@"联系电话:%ld",(long)_model.studentPhone];
}
- (NSMutableAttributedString *)returnAttributeWithTitle:(NSString *)title{
    NSMutableAttributedString *titleAttribute = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@",title]];
    //[titleAttribute setAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#666666"],NSFontAttributeName:[UIFont systemFontOfSize:LoginFont-3]} range:NSMakeRange(0, title.length)];
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    attachment.image = [UIImage imageNamed:@"TeachSignerTypeImage"];
    attachment.bounds = CGRectMake(0, -8*SCALEUNIT, 35*SCALEUNIT,35*SCALEUNIT );
    
    NSAttributedString *attributestr = [NSAttributedString attributedStringWithAttachment:attachment];
    [titleAttribute insertAttributedString:attributestr atIndex:0];
    return titleAttribute;
}

#pragma mark ==========拨打电话==========
- (void)phoneOpen:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(teachSignerTwoCellDelegatePhoneWithNum:)]) {
        [self.delegate teachSignerTwoCellDelegatePhoneWithNum:[NSString stringWithFormat:@"%ld",(long)_model.studentPhone]];
    }
}




- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
